uniform sampler2D frame;
uniform sampler2D pass;

const float base_blur_radius = 0.04;
const float blur_falloff = 0.04;
const float R = 0.08;

varying vec2 TexCoord;

#define M 12
const vec2 taps[M] =
{
    vec2(-0.326212,-0.40581),
    vec2(-0.840144,-0.07358),
    vec2(-0.695914,0.457137),
    vec2(-0.203345,0.620716),
    vec2(0.96234,-0.194983),
    vec2(0.473434,-0.480026),
    vec2(0.519456,0.767022),
    vec2(0.185461,-0.893124),
    vec2(0.507431,0.064425),
    vec2(0.89642,0.412458),
    vec2(-0.32194,-0.932615),
    vec2(-0.791559,-0.59771)
};

void main(void)
{
	vec4 color = texture2D(frame, TexCoord);
    float c = base_blur_radius * 2.0 * R * (texture2D(pass, TexCoord).r - 0.5);

    float amount = 1;
  
    for (int i = 0; i < M; i++)
    {
        vec2 dir = taps[i];

        vec2 s_tex = TexCoord + dir * c;
        vec4 s_color = texture2D(frame, s_tex);
        float s_c = (texture2D(pass, s_tex).r - 0.5) * 2.0 * R;
        float weight = min(exp2(-(c-s_c) / blur_falloff), 2.0);

        color += s_color * weight;
        amount += weight;
    }

    gl_FragColor = vec4(color.rgb / amount, 1.0);
}